package bb.lanxing.activity.bluetooth.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import androidx.appcompat.app.AlertDialog;
import androidx.databinding.ViewDataBinding;
import androidx.exifinterface.media.ExifInterface;
//import com.alipay.sdk.packet.d;
import com.umeng.analytics.pro.an;
import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.DeviceContext;
import bb.lanxing.lib.devices.ble.BleCons;
import bb.lanxing.lib.devices.ble.ble.characteristic.BodySensorLocation;
import bb.lanxing.lib.devices.ble.ble.characteristic.SensorLocation;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.utils.DeviceVersionHelper;
import bb.lanxing.model.database.Device;
import bb.lanxing.mvvm.base.BaseDBActivity;
import bb.lanxing.view.BiciAlertDialogBuilder;
import java.io.Serializable;
import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;

public abstract class BaseSensorActivity<T extends ViewDataBinding> extends BaseDBActivity<T> implements ConnectionListener {
    public static final String DEVICE_ADDRESS = "DEVICE_ADDRESS";
    public static final String DEVICE_NAME = "DEVICE_NAME";
    public static final String DEVICE_TYPE = "DEVICE_TYPE";

    private Device device;
    private String deviceAddress;
    private boolean isActive;
    private Integer deviceType = 2;
    private String firmwareVersion = "";
    private String serialNumber = "";
    private String model = "";
    private String manufacture = "";
    private boolean isShowDisconnect = true;
    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Serializable serializableExtra;

            String action = intent.getAction();
            if (action != null) {
                switch (action) {
                    case BleCons.ACTION_DEVICE_INFORMATION -> {
                        intent.getIntExtra(BleCons.EXTRA_DEVICE_TYPE, 0);
                        String stringExtra = intent.getStringExtra(BleCons.EXTRA_FIRMWARE_VERSION);
                        if (stringExtra != null) {
                            onGetDeviceFirmwareVersion(stringExtra);
                        }
                        String stringExtra2 = intent.getStringExtra(BleCons.EXTRA_MODEL);
                        if (stringExtra2 != null) {
                            onGetDeviceModel(stringExtra2);
                            if (getDevice() != null) {
                                Device device = getDevice();
                                Intrinsics.checkNotNull(device);
                                DeviceVersionHelper.setModelVersion(context, device.getAddress(), stringExtra2);
                            }
                        }
                        String stringExtra3 = intent.getStringExtra(BleCons.EXTRA_MANUFACTURE);
                        if (stringExtra3 != null) {
                            onGetDeviceManufacture(stringExtra3);
                        }
                        int intExtra = intent.getIntExtra(BleCons.EXTRA_CSC_FUTURE, -1);
                        if (intExtra != -1) {
                            onDeviceCSCFuture(intExtra);
                        }
                        String stringExtra4 = intent.getStringExtra(BleCons.EXTRA_SERIAL);
                        if (stringExtra4 == null) {
                            return;
                        }
                        onGetDeviceSerial(stringExtra4);
                    }
                    case BleCons.ACTION_HERATRATE_ALERT_WARN_CLOSE_STATUS -> {
                        onHrAlertDisable(intent.getBooleanExtra(BleCons.EXTRA_HERATRATE_BELT_WARN_CLOSE_STATUS, false));
                    }
                    case BleCons.ACTION_READ_DEVICE_INFORMATION_RESULT -> {
                        String stringExtra5 = intent.getStringExtra(BleCons.EXTRA_MANUFACTURE);
                        String stringExtra6 = intent.getStringExtra(BleCons.EXTRA_FIRMWARE_VERSION);
                        String stringExtra7 = intent.getStringExtra(BleCons.EXTRA_MODEL);
                        String stringExtra8 = intent.getStringExtra(BleCons.EXTRA_SERIAL);
                        onGetDeviceBattery(intent.getIntExtra(BleCons.EXTRA_BATTERY, 0));
                        if (stringExtra5 != null) {
                            onGetDeviceManufacture(stringExtra5);
                        }
                        if (stringExtra6 != null) {
                            onGetDeviceFirmwareVersion(stringExtra6);
                        }
                        if (stringExtra7 != null) {
                            onGetDeviceModel(stringExtra7);
                        }
                        if (stringExtra8 == null) {
                            return;
                        }
                        onGetDeviceSerial(stringExtra8);
                    }
                    case BleCons.ACTION_HERATRATE_ALERT_SUPPORT -> {
                        onHrAlertSupport(intent.getBooleanExtra(BleCons.EXTRA_HR_ALERT_FUTURE, false));
                    }
                    case BleCons.ACTION_HERATRATE_ALERT_WARN_SET_STATUS -> {
                        onHrAlertEnable(intent.getBooleanExtra(BleCons.EXTRA_HERATRATE_BELT_WARN_SET_STATUS, false));
                    }
                    case BleCons.ACTION_BATTERY -> {
                        int intExtra2 = intent.getIntExtra(BleCons.EXTRA_DEVICE_TYPE, 0);
                        Integer deviceType = getDeviceType();
                        if (deviceType == null || intExtra2 != deviceType) {
                            return;
                        }
                        onGetDeviceBattery(intent.getIntExtra(BleCons.EXTRA_BATTERY, 0));
                    }
                    case BleCons.ACTION_BODY_SENSOR_LOCATION -> {
                        serializableExtra = intent.getSerializableExtra(BleCons.EXTRA_BODY_SENSOR_LOCATION);
                        if (serializableExtra != null) {
                            onBodySensorLocation((BodySensorLocation) serializableExtra);
                        }
                    }
                    case BleCons.ACTION_SENSOR_LOCATION -> {
                        serializableExtra = intent.getSerializableExtra(BleCons.EXTRA_SENSOR_LOCATION);
                        if (serializableExtra != null) {
                            onSensorLocation((SensorLocation) serializableExtra);
                        }
                    }
                    case BleCons.ACTION_HERATRATE_ALERT_WARN -> {
                        boolean booleanExtra = intent.getBooleanExtra(BleCons.EXTRA_HERATRATE_BELT_WARN_STATUS, false);
                        String stringExtra9 = intent.getStringExtra(BleCons.EXTRA_HERATRATE_BELT_WARN);
                        if (stringExtra9 != null) {
                            onGetHrAlertInfo(booleanExtra, Float.parseFloat(stringExtra9));
                        }
                    }
                    default -> {
                    }
                }
            }
        }
    };

    public void deleteDevice() {
    }

    public void onBodySensorLocation(BodySensorLocation bodySensorLocation) {
//        Intrinsics.checkNotNullParameter(bodySensorLocation, "bodySensorLocation");
    }

    public void onDeviceAlarmDisable(boolean z) {
    }

    public void onDeviceAlarmSupport(boolean z) {
    }

    public void onDeviceCSCFuture(int i) {
    }

    public void onDeviceConnecting() {
    }

    public void onDeviceDisConnected() {
    }

    public void onDeviceRefresh(Device device) {
//        Intrinsics.checkNotNullParameter(device, "device");
    }

    public void onGetDeviceBattery(int i) {
    }

    public void onGetHrAlertInfo(boolean z, float f) {
    }

    public void onHrAlertDisable(boolean z) {
    }

    public void onHrAlertEnable(boolean z) {
    }

    public void onHrAlertSupport(boolean z) {
    }

    public void onSensorLocation(SensorLocation sensorLocation) {
//        Intrinsics.checkNotNullParameter(sensorLocation, "sensorLocation");
    }

    public final Device getDevice() {
        return this.device;
    }

    protected final void setDevice(Device device) {
        this.device = device;
    }

    public final String getDeviceAddress() {
        return this.deviceAddress;
    }

    protected final void setDeviceAddress(String str) {
        this.deviceAddress = str;
    }

    public final Integer getDeviceType() {
        return this.deviceType;
    }

    protected final void setDeviceType(Integer num) {
        this.deviceType = num;
    }

    public final boolean isShowDisconnect() {
        return this.isShowDisconnect;
    }

    public final void setShowDisconnect(boolean z) {
        this.isShowDisconnect = z;
    }

    @Override
    public void initView(T t) {
        this.deviceType = getIntent().getIntExtra(BleCons.EXTRA_DEVICE_TYPE, SmartDevice.TYPE_CADENCE);
        String stringExtra = getIntent().getStringExtra(BleCons.EXTRA_DEVICE_ADDRESS);
        this.deviceAddress = stringExtra;
        this.device = Device.getByAddress(stringExtra);
        setupActionBar(true);
        Device device = this.device;
        setTitle(device != null ? device.getName() : null);
        if (this.device == null) {
            finish();
        }
        DeviceHelper.registerConnectionStateListener(this);
        registerReceiver();
    }

    @Override
    public void onResume() {
        super.onResume();
        Device device = this.device;
        String address = device != null ? device.getAddress() : null;
        if (address == null) {
            address = "";
        }
        if (!DeviceHelper.isConnected(address)) {
            DeviceHelper.connect(this.device);
            onDeviceConnecting();
            return;
        }
        onDeviceConnected();
    }

    public void onDeviceConnected() {
        Device device = this.device;
        Intrinsics.checkNotNull(device);
        onDeviceRefresh(device);
        readDeviceInfo();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(this.broadcastReceiver);
        DeviceHelper.unregisterConnectionStateListener(this);
    }

    @Override
    public void onConnectionStateChanged(SmartDevice smartDevice, int i, int i2) {
        String str = null;
        if (Intrinsics.areEqual(smartDevice != null ? smartDevice.getType() : null, this.deviceType)) {
            String address = smartDevice != null ? smartDevice.getAddress() : null;
            Device device = this.device;
            if (device != null) {
                str = device.getAddress();
            }
            if (!Intrinsics.areEqual(address, str)) {
                return;
            }
            if (i == DeviceManager.STATE_CONNECTED) {
                onDeviceConnected();
            } else if (i == DeviceManager.STATE_DISCONNECTED) {
                dismissLoadingDialog();
                if (this.isShowDisconnect) {
                    toast((int) R.string.st_disconnected);
                }
                onDeviceDisConnected();
            }
        }
    }

    public void onGetDeviceFirmwareVersion(String firmwareVersion) {
//        Intrinsics.checkNotNullParameter(firmwareVersion, "firmwareVersion");
        this.firmwareVersion = firmwareVersion;
    }

    public void onGetDeviceModel(String model) {
//        Intrinsics.checkNotNullParameter(model, "model");
        this.model = model;
    }

    public void onGetDeviceManufacture(String manufacture) {
//        Intrinsics.checkNotNullParameter(manufacture, "manufacture");
        this.manufacture = manufacture;
    }

    public void onGetDeviceSerial(String serial) {
        this.serialNumber = serial;
    }

    public void unBoundDevice() {
        AlertDialog show =
                new BiciAlertDialogBuilder(this)
                        .setTitle(R.string.st_forget_sensor)
                        .setMessage(R.string.st_are_you_sure_unbind)
                        .setPositiveButton(R.string.mine_profile_dialog_btn_unbind, (dialogInterface, i) -> unBoundDevice(this, dialogInterface, i))
                        .setNegativeButton(R.string.str_sync_cancel, (dialog, which) -> {
                            //empty
                        })
                        .show();

        show.setCanceledOnTouchOutside(true);
    }


    public static void unBoundDevice(BaseSensorActivity activity, DialogInterface dialogInterface, int i) {
        String str = activity.deviceAddress;
        if (str != null) {
            Device.deleteAll(Device.class, "address = ? and userId = ?", str, String.valueOf(App.getContext().getUserId()));
            DeviceHelper.disconnect(activity.deviceAddress);
        }
        DeviceContext.startAndRefreshAutoConnector();
        activity.finish();
    }

    public void registerReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BleCons.ACTION_BATTERY);
        intentFilter.addAction(BleCons.ACTION_DEVICE_INFORMATION);
        intentFilter.addAction(BleCons.ACTION_CADENCE_DATAS);
        intentFilter.addAction(BleCons.ACTION_PRESSURE_DATAS);
        intentFilter.addAction(BleCons.ACTION_READ_DEVICE_INFORMATION_RESULT);
        intentFilter.addAction(BleCons.ACTION_HERATRATE_ALERT_SUPPORT);
        intentFilter.addAction(BleCons.ACTION_HERATRATE_ALERT_WARN);
        intentFilter.addAction(BleCons.ACTION_HERATRATE_ALERT_WARN_SET_STATUS);
        intentFilter.addAction(BleCons.ACTION_HERATRATE_ALERT_WARN_CLOSE_STATUS);
        intentFilter.addAction(BleCons.ACTION_BODY_SENSOR_LOCATION);
        intentFilter.addAction(BleCons.ACTION_SENSOR_LOCATION);
        registerReceiver(this.broadcastReceiver, intentFilter, Context.RECEIVER_EXPORTED);
    }

    public void requestBattery() {
        if (this.deviceAddress != null) {
            Intent intent = new Intent(BleCons.ACTION_REQUEST_BATTERY);
            intent.putExtra("EXTRA_DEVICE_TYPE", this.deviceType);
            intent.putExtra("EXTRA_DEVICE_ADDRESS", this.deviceAddress);
            sendOrderedBroadcast(intent, null);
        }
    }

    public void requestDeviceInfo() {
        if (this.deviceAddress != null) {
            Intent intent = new Intent(BleCons.ACTION_REQUEST_DEVICE_INFORMATION);
            intent.putExtra("EXTRA_DEVICE_TYPE", this.deviceType);
            intent.putExtra("EXTRA_DEVICE_ADDRESS", this.deviceAddress);
            sendOrderedBroadcast(intent, null);
        }
    }

    public void readDeviceInfo() {
        if (this.deviceAddress != null) {
            Intent intent = new Intent(BleCons.ACTION_READ_DEVICE_INFORMATION);
            intent.putExtra(BleCons.EXTRA_DEVICE_TYPE, this.deviceType);
            intent.putExtra(BleCons.EXTRA_DEVICE_ADDRESS, this.deviceAddress);
            sendOrderedBroadcast(intent, null);
        }
    }
}
